<template>
	<view class="">
		<view class="">
			<view class="top ">
				<!-- 用户资料 -->
				<view class="user flex-col-between">
					<view class="user-info flex-left">
						<image class="user-avatar" :src="spreadInfo.headimg" mode="aspectFit"></image>
						<view class="">
							<view class="flex-left">
								<view class="user-name">{{spreadInfo.nickname}}</view>
								<view class="user-level">{{spreadInfo.levelName}}</view>
							</view>
							<view class="user-recommend">推荐人：{{spreadInfo.sourceName}}</view>
						</view>
					</view>
					<image class="user-code" 
					@click="linkTo('./invite-user')" 
					:src="$util.img('upload/uniapp/spread/erweima.png')" mode="aspectFit"></image>
				</view>
				<!-- 数字资产统计 -->
				<view class="stats-intergral flex-col-between">
					<view class="stats-field" v-for="(item,index) in statsFields" :key="index">
						<view class="stats-title">{{spreadInfo[item.index]}}</view>
						<view class="stats-value">{{item.title}}</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 提现模块 -->
		<view class="withdraw">
			<view class="withdraw-top" :style="{backgroundImage: 'url('+$util.img('upload/uniapp/spread/ext-bgm.png')+')'}">
				<view class="withdraw-title flex-left">可提现<view class="withdraw-amount">{{spreadInfo.balance_money}}</view>
				</view>
				<view class="withdraw-butt" @click="linkTo('/otherpages/member/apply_withdrawal/apply_withdrawal')">申请提现</view>
			</view>
			<!-- <view class="withdraw-stats">
				<view class="stats" v-for="(item,index) in amountFields" :key="index">
					<view class="stats-title">{{spreadInfo[item.index]}}</view>
					<view class="stats-value">{{item.title}}</view>
				</view>
			</view> -->
		</view>
		<!-- 我的推广 -->
		<view class="box">
			<view class="box-title">我的推广</view>
			<view class="box-content flex-col-between">
				<view class="extension-stats" 
				@click="linkTo(item.link,item.is_extension)" v-for="(item,index) in extensionFields"
				:style="{backgroundImage: 'url('+$util.img('upload/uniapp/spread/'+item.icon)+')'}"
					:key="index">
					<view class="stats">
						<view class="stats-title">{{spreadInfo[item.index]}}</view>
						<view class="stats-value">{{item.title}}</view>
					</view>
					<!-- <image class="stats-icon" :src="item.icon" mode="aspectFit"></image> -->
				</view>
			</view>
		</view>
		<!-- 常用工具 -->
		<view class="box menus">
			<view class="box-title">常用工具</view>
			<view class="box-content menus-content flex-left">
				<view class="menu" @click="linkTo(item.link,item.is_extension)" v-for="(item,index) in menuList" :key="index">
					<image class="menu-icon" :src="$util.img('upload/uniapp/spread/'+item.icon)" mode="aspectFit"></image>
					<view class="menu-title">{{item.title}}</view>
				</view>
			</view>
		</view>
		<loading-cover ref="loadingCover"></loading-cover>
	</view>
</template>

<script>
	import globalConfig from '@/common/js/golbalConfig.js';
	export default {
		data() {
			return {
				statsFields: [{
					title: '可用数字资产',
					index: 'point'
				}, {
					title: '累计数字资产',
					index: 'settled'
				}, {
					title: '结算中数字资产',
					index: 'beSettled'
				}],
				amountFields: [{
					title: '今日销售额(元)',
					index: 'daySale'
				}, {
					title: '总销售额(元)',
					index: 'totalSale'
				}],
				extensionFields: [{
					title: '推广用户',
					index: 'tgMember',
					icon: 'bgm-ext-user.png',
					link: './extension-user'
				}, {
					title: '推广商家',
					index: 'tgShop',
					icon: 'bgm-ext-record.png',
					link: './extension-record',
					is_extension: 1
				}],
				menuList: [{
					title: '我的数字资产',
					icon: 'ext-menu-point.png',
					link: './my-point'
				}, {
					title: '我的分红',
					icon: 'ext-menu-bonus.png',
					link: './my-bonus'
				}, {
					title: '结算订单',
					icon: 'ext-menu-order.png',
					link: './extension-order'
				}, {
					title: '邀请用户',
					icon: 'ext-menu-user.png',
					link: './invite-user'
				}, {
					title: '邀请商家',
					icon: 'ext-menu-shop.png',
					link: './invite-shop',
					is_extension: 1
				}],
				spreadInfo: {}
			};
		},
		mixins: [globalConfig],
		methods: {
			linkTo(url,is_extension=0) {
				if (this.spreadInfo.is_extension == 0 && is_extension == 1) {
					this.$util.showToast({
						title: '您当前用户等级暂不可操作推广商户功能，请升级用户等级',
						mask: true
					});
					return 
				}
				uni.navigateTo({
					url: url
				})
			},
			getSpreadDetail() {
				this.$api.sendRequest({
					url: '/spread/api/spread/management',
					success: res => {
						if (res.data) {
							if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
							this.spreadInfo = res.data;
						}
					},
					fail: res => {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			}
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();
			if (!this.addonIsExit.spread) {
				this.$util.showToast({
					title: '推广中心未开启',
					mask: true
				});
				setTimeout(() => {
					this.$util.redirectTo('/pages/index/index/index', {}, 'redirectTo');
				}, 1000);
			} else {
				if (uni.getStorageSync('token')) {
					this.getSpreadDetail();
				} else {
					this.$util.redirectTo(
						'/otherpages/login/login/login',
						{
							back: '/otherpages/promotion/index'
						},
						'redirectTo'
					);
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.flex-col-between {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.flex-left {
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}

	.top {
		padding: 48rpx;
	}

	.user {
		.user-info {
			.user-avatar {
				margin-right: 20rpx;
				width: 120rpx;
				height: 120rpx;
				border-radius: 120rpx;
			}

			.user-name {
				font-size: 32rpx;
				color: #303133;
				line-height: 45rpx;
			}

			.user-level {
				margin-left: 12rpx;
				font-size: 24rpx;
				color: #D9C184;
				height: 34rpx;
				line-height: 34rpx;
				background: #955836;
				padding: 0 10rpx;
				border-radius: 20rpx;
			}

			.user-recommend {
				margin-top: 12rpx;
				font-size: 24rpx;
				color: #909399;
				line-height: 33rpx;
			}
		}
		.user-code {
			width: 48rpx;
			height: 48rpx;
		}
	}


	.stats-intergral {
		margin-top: 48rpx;
	}
	
	.stats-field {
		
	}
	.stats-title {
		font-size: 36rpx;
		font-family: DINAlternate-Bold, DINAlternate;
		font-weight: bold;
		color: #303133;
		line-height: 42rpx;
	}
		
	.stats-value {
		margin-top: 8rpx;
		font-size: 24rpx;
		color: #909399;
		line-height: 34rpx;
	}

	.withdraw {
		margin-left: 30rpx;
		width: 690rpx;
		border-radius: 16rpx;
		overflow: hidden;
		background-color: #fff;

		.withdraw-top {
			background-color: red;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 30rpx;
			height: 104rpx;
			background-repeat: no-repeat;
			background-size: 100%;

			.withdraw-title {
				font-size: 24rpx;
				color: #FFFFFF;
				line-height: 34rpx;

				.withdraw-amount {
					margin-left: 12rpx;
					font-size: 36rpx;
					font-family: DINAlternate-Bold, DINAlternate;
					font-weight: bold;
					color: #FFFFFF;
					line-height: 42rpx;
				}
			}

			.withdraw-butt {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 128rpx;
				height: 52rpx;
				background: #FFFFFF;
				border-radius: 31rpx;
				font-size: 24rpx;
				font-weight: 400;
				color: #FF4544;
				line-height: 33rpx;
			}
		}

		.withdraw-stats {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 30rpx;
			padding-bottom: 40rpx;

			.stats {
				
				.stats-title {}

				.stats-value {}
			}
		}
	}


	.box {
		margin: 30rpx;
		width: 690rpx;
		background: #FFFFFF;
		border-radius: 16rpx;

		.box-title {
			padding: 24rpx 30rpx;
			font-size: 32rpx;
			font-family: PingFangSC-Semibold, PingFang SC;
			font-weight: 600;
			color: #303133;
			line-height: 46rpx;
		}

		.box-content {
			padding: 24rpx 20rpx;
			border-top: 1rpx solid #E8E8E8;
		}
	}

	.extension-stats {
		.stats {
			box-sizing: border-box;
			padding: 24rpx 30rpx;
			width: 315rpx;
			height: 132rpx;
		}
		&:first-child {
			background-size: 100%;
		}
		&:last-child {
			background-size: 100%;
		}
	}
	.menus {
		.menus-content {
			padding: 20rpx 40rpx;
			flex-wrap: wrap;
		}
	}
	
	.menu {
		margin-right: 75rpx;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
		&:nth-child(4n){
			margin-right: 0;
		}
		.menu-icon {
			margin-left: 24rpx;
			width: 48rpx;
			height: 48rpx;
		}
		.menu-title{
			text-align: center;
			white-space: nowrap;
			margin-top: 16rpx;
			font-size: 24rpx;
			color: #303133;
			line-height: 34rpx;
		}
	}
	
</style>
